package com.cjd.common.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cjd.common.holder.BaseRecyclerHolder


/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/7/20
 * description （快速实现adapter 无法自定义ViewType）
 */
abstract class BaseRecyclerQuickAdapter<T>(private val mContext: Context, private val layoutResId: Int) :
    BaseRecyclerArrayAdapter<T, BaseRecyclerHolder>() {

    var listener: ((view: View, pos: Int) -> Unit)? = null

    fun setOnItemClickListener(block: ((view: View, pos: Int) -> Unit)) {
        listener = block
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): BaseRecyclerHolder {
        val view = LayoutInflater.from(mContext).inflate(layoutResId, viewGroup, false)
        val baseHolder = BaseRecyclerHolder(view)
        view.setOnClickListener {
            listener?.invoke(it, baseHolder.layoutPosition)
        }
        return baseHolder
    }

    override fun onBindViewHolder(baseHolder: BaseRecyclerHolder, position: Int) {
        convert(baseHolder, getItem(position))
    }

    protected abstract fun convert(baseHolder: BaseRecyclerHolder, item: T)

    public fun isSingleRow(pos: Int): Boolean {
        return false
    }
}